﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BAL.Entities;
using DAL.SQLStatements;
using BAL.Utils;
using System.Data;

namespace DAL.LogicComponents
{
    public class clsProducerDAL
    {
        #region --- Add new Producer
        public static bool queryAddProducer(clsProducer obj)
        {
            // Add value to column(s)
            Dictionary<String, Object> data = new Dictionary<String, Object>();
            data.Add("colCategory", "N'" + obj.ProducerName + "'");
            data.Add("colDescription", "N'" + obj.Description + "'");
            data.Add("colAvatar", "N'" + obj.Avatar + "'");
            data.Add("colInsertDate", "'" + clsDateTimeSetting.UTCTime() + "'");
            data.Add("colIsTrash", 1);

            //Execute insert query
            clsQueryBuilder query = new clsQueryBuilder();
            return query.executeInsertInto("tblProducer", data);
        }
        #endregion


        #region--- Update Producer
        public static bool queryUpdateProducer(clsProducer obj)
        {
            Dictionary<String, Object> cols = new Dictionary<String, Object>();
            cols.Add("colProducer", obj.ProducerName);
            cols.Add("colDescription", obj.Description);
            if (obj.Avatar != null)
            {
                cols.Add("colAvatar", obj.Avatar);
            }
            cols.Add("colLastUpdate", clsDateTimeSetting.UTCTime());
            // Execute update query
            clsQueryBuilder query = new clsQueryBuilder();
            return query.executeUpdate("tblProducer", cols, String.Format("colProducerID = {0}", obj.ProducerID));

        }
        #endregion

        #region--- Select Producrt List
        public static DataTable querySelectProducerList()
        {
            clsQueryBuilder query = new clsQueryBuilder();
            query.propTable = "tblProducer";
            query.propColumn = "*";
            query.propWhere = "colIsTrash = 1";

            return query.getDatatable();
        }
        #endregion ---

        #region --- Select producer by id
        public static DataTable querySelectProducer(clsProducer obj)
        {
            clsQueryBuilder query = new clsQueryBuilder();
            query.propTable = "tblProducer";
            query.propColumn = "colProducerID,colProducer,colDescription,colAvatar,colInsertDate,colLastUpdate";
            query.propWhere = "colIsTrash= 1 and colProducerID ='" + obj.ProducerID + "'";

            return query.getDatatable();
        }
        #endregion

        #region --- Delete Product By User
        public static bool queryDeleteProducer(clsProducer obj)
        {
            Dictionary<String, Object> data = new Dictionary<String, Object>();
            data.Add("colIsTrash", 0);
            string where = "colProducerID='" + obj.ProducerID + "'";

            // New update query
            clsQueryBuilder query = new clsQueryBuilder();

            // Execute query
            return query.executeUpdate("tblProducer", data, where);
        }
        #endregion

        #region -- check Producer Exist
        public static bool CheckProducerExist(clsProducer obj)
        {
            clsQueryBuilder query = new clsQueryBuilder();
            query.propColumn = "colProducer";
            query.propTable = "tblProducer";
            query.propWhere = "colProducerID > 0 and colProducer = N'"+obj.ProducerName+"'";

            return query.getBoolValue();
        }
        #endregion
    }
    
}